﻿// See https://aka.ms/new-console-template for more information
    ///输出2022年日历
    //定义今年的第一天
    string dateInput = "01 01, 2022";
    var parsedDate = DateTime.Parse(dateInput);
    int Thisyear = parsedDate.Year;
    //如果是闰年则Isleapyear等于0，否则为1
    bool isleapyear = Thisyear % 4 == 0 ;
    int leapyear = Convert.ToInt32(isleapyear);
    //取一个今年每月的日数的数组
    int[] days = { 31, 28 + leapyear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    int x = 0, y = 0;
    //循环体
for (int i = 0; i < 4; i++)//1行3个月
{
    for (int j = 0; j < 3; j++)
    {
        //定位光标位置
        x = 10+35 * j;
        y = 10 * i;
        Console.SetCursorPosition(x, y);
        //输出月份
        int a = j + 1 + 3 * i;
        Console.Write(a);
        Console.Write("月");
        //定位光标位置
        x = 35 * j ;
        y = 10 * i + 1;
        Console.SetCursorPosition(x, y);
        //输出星期
        Console.Write(new String(" 日 一 二 三 四 五 六"));
    }
    //每一行的循环体
    for (int j = 0; j < 3; j++)
    {
        x = 35 * j + 1;
        y = 10 * i + 2;
        Console.SetCursorPosition(x, y);
        int n = (int)parsedDate.DayOfWeek;
        Console.Write(new String(' ', 3 * n));
        //每个月输出天数
        for (int k = 0; k < days[j + 3 * i]; k++)
        {
            //输入这一行的日期
            if (k + 1 < 10) 
            Console.Write(" ");
            Console.Write(k + 1);
            Console.Write(" ");
            //判断当前是星期几
            n = (int)parsedDate.DayOfWeek;
            //如果当前是星期六，光标移到下一行第一列;
            if (n == 6)
            Console.SetCursorPosition(x, ++y);
            //接下来的日期+1
            parsedDate = parsedDate.AddDays(1);
        }
    }
}
    